package com.supermarket.manage.provider.message.service;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.supermarket.manage.common.constant.CodeConstant;
import com.supermarket.manage.common.dto.PhoneCodeResponse;
import com.supermarket.manage.common.utils.CodeUtil;
import com.supermarket.manage.provider.api.message.CodeService;
import com.supermarket.manage.provider.message.config.CodeConfig;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author Faxon
 * @version 1.0
 * @date 2020-03-10 19:54
 **/
@Service(version = "1.0.0", timeout = 5000)
public class CodeServiceImpl implements CodeService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Boolean sendCodeByPhoneNumber(String phoneNumber, String modelId) {
        //生成验证码
        String code = CodeUtil.getCode();

        DefaultProfile profile = DefaultProfile.getProfile(CodeConfig.REGION_ID, CodeConfig.ACCESSKEY_ID, CodeConfig.ACCESSKEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(CodeConfig.SEND_DOMAIN);
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", CodeConfig.REGION_ID);
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", CodeConfig.SIGN_NAME);
        request.putQueryParameter("TemplateCode", modelId);
        request.putQueryParameter("TemplateParam", "{code:\"" + code + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            if (response != null && StringUtils.isNotBlank(response.getData())) {
                PhoneCodeResponse data = JSON.parseObject(response.getData(), PhoneCodeResponse.class);
                if (data.getCode().equals(CodeConfig.SEND_OK)) {
                    stringRedisTemplate.opsForValue().set(CodeConstant.CODE_PREFIX + phoneNumber, code, 5, TimeUnit.MINUTES);
                    return Boolean.TRUE;
                }
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean checkPhoneCode(String phoneNumber, String code) {
        String value = stringRedisTemplate.opsForValue().get(CodeConstant.CODE_PREFIX + phoneNumber);
        if (value != null && value.equals(code)) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
}
